package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.ProjectFileMapper;
import org.linlinjava.litemall.db.domain.ProjectFile;
import org.linlinjava.litemall.db.domain.ProjectFileExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/4 12:04
 * @Version: 1.0
 * @Description:
 */
@Service
public class ProjectFileService {

    @Resource
    private ProjectFileMapper projectFileMapper;

    public int insert(ProjectFile projectFile) {
        return projectFileMapper.insert(projectFile);
    }

    public List<ProjectFile> selectByProjectId(String projectId) {
        ProjectFileExample projectFileExample = new ProjectFileExample();
        ProjectFileExample.Criteria criteria = projectFileExample.createCriteria();
        criteria.andProjectIdEqualTo(projectId);
        return projectFileMapper.selectByExample(projectFileExample);
    }

    // TODO 这个删除的时候需要同步删除文件，后面看下怎么做
    public int deleteByProjectId(String projectId) {
        ProjectFileExample projectFileExample = new ProjectFileExample();
        ProjectFileExample.Criteria criteria = projectFileExample.createCriteria();
        criteria.andProjectIdEqualTo(projectId);
        // storageService.delete(fileName);
        return projectFileMapper.deleteByExample(projectFileExample);
    }

    public ProjectFile selectByFileSaveName(String fileSaveName) {
        ProjectFileExample example = new ProjectFileExample();
        ProjectFileExample.Criteria criteria = example.createCriteria();
        criteria.andFileSaveNameEqualTo(fileSaveName);
        ProjectFile projectFile = projectFileMapper.selectOneByExample(example);
        return projectFile;
    }

    public ProjectFile selectByFileNameAndProjectId(String fileName, String projectId) {
        ProjectFileExample example = new ProjectFileExample();
        ProjectFileExample.Criteria criteria = example.createCriteria();
        criteria.andProjectIdEqualTo(projectId);
        criteria.andFileNameEqualTo(fileName);
        ProjectFile projectFile = projectFileMapper.selectOneByExample(example);
        return projectFile;
    }

    public int updateByPrimaryKey(ProjectFile projectFile) {
        return projectFileMapper.updateByPrimaryKeySelective(projectFile);
    }
}
